typedef struct {
    int key;
    int idx;
    UT_hash_handle hh;
} uthash_t;

#define MAX(A,B) ((A)>(B)? (A):(B))
int lengthOfLongestSubstring(char* s){
    uthash_t *hash = NULL, *ret = NULL;
    int left = 0, right = 0, len = strlen(s); //[left, right]
    int maxlen = 0;
    while(right < len) {
        
        int tmp = s[right];
        HASH_FIND_INT(hash, &tmp, ret);
        if (ret == NULL) {
            uthash_t *node = malloc(sizeof(*node));
            node->key = tmp;
            node->idx = right;
            HASH_ADD_INT(hash, key, node);
        } else {    //存在
            for (int i = left; i <= ret->idx-1; i++) { //滑动前将重复元素前的内容删除，不删除重复元素，只更新其下标
                uthash_t *del = NULL;	//这里不能再用ret作临时变量
                int tmp = s[i];
                HASH_FIND_INT(hash, &tmp, del);
                if (del) {
                    HASH_DEL(hash,del);
                    free(del);
                }          
            }
            left = ret->idx+1;//更新左边界
            ret->idx = right; //更新重复元素的下标 
        }
        maxlen = MAX(maxlen, right - left + 1);    //是否需要更新长度 
        right++;
    }
    // uthash_t *next;
    // HASH_ITER(hh, hash, ret, next) {
    //     HASH_DEL(hash, ret);
    // }
    return maxlen;
}